package taliav3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

/**
 *
 *
 * Tworzenie menu nowej gry.
 * Mozliwe jest wywolanie menu pomocy,
 * mozna zmienic rowniez talie/rewers/stol
 *
 */
public class MainMenu{

    protected JFrame MainW;
    protected JPopupMenu PopUp;
    protected JLayeredPane MainP;
    protected JLabel BG, exit,titlebar,minimize,menu[],server,klient,ok,PopUpImage;
    protected JButton rewersy[], stoly[], talie[];
    protected ImageIcon resized[],decks[];
    protected static Point point = new Point();
    protected String serwerPath;
    protected int rewers, talia, jokers, port;
    protected boolean serwer, ip_address;
    protected String path,serw;
    protected JTextField SerwerPathArea;
    public MainMenu()
    {
        port=2004;
        rewers=3;
        talia=1;
        jokers=3;
        path= "/taliav3/img/Stoly/stol5.jpg";
        serw="localhost";
        Draw();
    }
    public MainMenu(int rewers, int talia, int jokers, String path, boolean serwer, int port, String SerwerPath)    /** zapamietywanie ustawien po skonczonej grze*/
    {
        this.rewers=rewers;
        this.talia=talia;
        this.jokers=jokers;
        this.path=path;
        this.serwer=serwer;
        this.port=port;
        if(SerwerPath.equals(""))
            serw="localhost";
        else
            this.serw=SerwerPath;
        Draw();
    }

    public MainMenu(String string,int rewers, int talia, int jokers, String path, boolean serwer, int port, String SerwerPath) {
       new MainMenu(rewers,talia,jokers,path,serwer,port,SerwerPath);
       JOptionPane.showMessageDialog(null, string);
    }
    public void Draw()
    {
        rewersy = new JButton[15];
        stoly = new JButton[12];
        resized = new ImageIcon[12];
        decks = new ImageIcon[3];
        talie = new JButton[3];
	serwerPath="";
        MainW = new JFrame();
        MainW.setSize(1143,700);
        MainW.setLocationRelativeTo(null);
        MainP = new JLayeredPane();
        MainP.setBounds(0,0,1143,700);
        exit = new JLabel();
        minimize = new JLabel();
        titlebar = new JLabel();
        BG = new JLabel();
        menu = new JLabel[6];
        for(int i=0; i<6; i++)
            menu[i] = new JLabel();
        BG.setOpaque(true);
        Labels.setLabel(BG, 0, 0, 1143, 700, "/taliav3/img/menu.jpg");

        Labels.setLabel(exit, 1112, 2, 30, 17, "/taliav3/img/close.png", MouseAdapters.closeFrame());
        Labels.setLabel(minimize, 1083, 2, 29, 17, "/taliav3/img/min.png", MouseAdapters.minimizeFrame(MainW));
        Labels.setLabel(titlebar, 0, 0, 1143, 21, "/taliav3/img/titlebar.png",MouseAdapters.getFrameLocation(this),MouseAdapters.moveFrame(this));

        MainW.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainW.setUndecorated(true);
        menu();
        for(int i=0; i<6; i++)
            MainP.add(menu[i]);
        MainP.add(exit);
        MainP.add(minimize);
        MainP.add(titlebar);

        MainP.add(BG);
        MainW.add(MainP);
        MainW.setVisible(true);
        MainW.addKeyListener(new KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                switch(evt.getKeyCode())
                {
                    case KeyEvent.VK_F1:        /** gdy wcisniety jest klawisz F1 wywolywana jest pomoc*/
                        help(1);
                        break;
                    case KeyEvent.VK_ESCAPE:    /** gdy wcisniety jest klawisz ESC rysowane jest ponownie menu*/
                        restoreMenu();
                        break;
                }
            }
        });
        MouseAdapters.closeFrame();
    }
    private void menu()
    {
        MainP.removeAll();
        String paths[]={"nowa","heart","opcje","heart","koniec","heartD"};
        String names[]={"nowa","heartN","opcje","heartO","koniec","heartD"};
        int sizes[]={188,16,75,77,111,16,75,77,213,16,75,77};
        int X=536, Y=176, mnoz=0;
        for(int i=0,j=0; i<6; i++,j+=2)
        {
            if(i%2==0)
            {
                if(i==0 || i==2)
                    mnoz=4;
                else
                    mnoz=2;
                X=536-sizes[i]/mnoz;
            }
            else
                X=536;
            String PathToFile="/taliav3/img/"+paths[i];
            menu[i].setName(names[i]);
            Labels.setLabel(menu[i], X, Y, sizes[j], sizes[j+1], ("/taliav3/img/"+paths[i]+".png"),MouseAdapters.pickMainMenu(this,PathToFile));
            menu[i].addMouseListener(MouseAdapters.pickMainMenu(this,PathToFile));
            Y+=sizes[j+1]+((j+1)%2)*4+(i%2)*68;
        }
    }
    protected void options()
    {
        String res_path;
        for(int i=0; i<12; i++)
        {
            res_path="/taliav3/img/Stoly/stol"+(i+1)+"m.jpg";
            resized[i] = new ImageIcon(getClass().getResource(res_path));
        }
        Font font1 = new Font("Century Gothic", Font.BOLD, 30);
        MainP.removeAll();
        PopUp = new JPopupMenu();
        PopUpImage = new JLabel();
        String rewers_nazwy[]={"False Just-In", "True Just-In", "Playmate", "Miami Vice", "Kowadło",
        "Abstrakcyjny", "Super-Grzanka", "Bizon", "Lost", "Klasyczny", "Simpsonowie",
        "Stworzenie Buttheada", "Conrholio", "Chewbacca", "Buka"};
        String stoly_nazwy[]={"Zielona dolina", "Czysta abstrakcja", "300-tu", "Marzenie senne", "102-B",
        "Krzesełko", "Quark", "Logo", "Suit", "Bubbly bubble", "Galaxy", "Zebra"};
        String talie_nazwy[]={"Klasyczna","Nietuzinkowa","Odkrywcza"};
        int x=150;
        JLabel rewersText = new JLabel();
        Labels.setLabel(rewersText, 400, 169, 400, 30, "REWERSY do wyboru:", font1);
        rewersText.setOpaque(false);
        MainP.add(rewersText);
        MainP.moveToFront(rewersText);
        for(int i=0;i<rewers_nazwy.length; i++)
        {
            rewersy[i] = new JButton();
            rewersy[i].setText(rewers_nazwy[i]);
            rewersy[i].setName(""+(i+1));
            int mnoznik=0;
            if(i>4 || i>9)
            {
                if(i==5 || i==10)
                    x=150;
                mnoznik=1;
                if(i>9)
                    mnoznik=2;
            }
            rewersy[i].setBounds(x, 200+40*mnoznik, 160, 30);
            rewersy[i].addMouseListener(new MouseAdapter(){
                    public void mouseClicked(MouseEvent me)
                    {
                        JButton button = (JButton) me.getComponent();
                        rewers=Integer.parseInt(button.getName());
                    }
                    public void mouseEntered(MouseEvent me)
                    {
                        PopUp.removeAll();                        
                        JButton button = (JButton) me.getComponent();
                        int rewers_nr=Integer.parseInt(button.getName());
                        PopUpImage.setIcon(new ImageIcon(getClass().getResource("/taliav3/img/Rewersy/rewers"+rewers_nr+".png")));
                        PopUpImage.setBounds(0, 0, 120, 150);
                        PopUp.add(PopUpImage);
                        PopUp.show(MainW, button.getX()-120, button.getY());
                    }
                    public void mouseExited(MouseEvent me)
                    {
                        PopUp.setVisible(false);
                    }
            });
            x+=164;
            MainP.add(rewersy[i]);
        }
        x=150;
        JLabel stolyText = new JLabel();
        stolyText.setOpaque(false);
        Labels.setLabel(stolyText, 400, 360, 400, 30, "STOŁY do wyboru:", font1);
        MainP.add(stolyText);
        for(int i=0;i<stoly_nazwy.length; i++)
        {
            stoly[i] = new JButton();
            stoly[i].setText(stoly_nazwy[i]);
            stoly[i].setName(""+(i+1));
            int mnoznik=0;
            if(i>4 || i>9)
            {
                if(i==5 || i==10)
                    x=150;
                mnoznik=1;
                if(i>9)
                    mnoznik=2;
            }
            stoly[i].setBounds(x, 390+40*mnoznik, 160, 30);
            stoly[i].addMouseListener(new MouseAdapter(){
                    public void mouseClicked(MouseEvent me)
                    {
                        JButton button = (JButton) me.getComponent();
                        path="/taliav3/img/Stoly/stol"+Integer.parseInt(button.getName())+".jpg";
                    }
                    public void mouseEntered(MouseEvent me)
                    {
                        PopUp.removeAll();                        
                        JButton button = (JButton) me.getComponent();
                        int stol_nr=Integer.parseInt(button.getName())-1;
                        PopUpImage.setIcon(resized[stol_nr]);
                        PopUp.add(PopUpImage);
                        PopUp.pack();
                        PopUp.show(MainW, 285, 30);
                    }
                    public void mouseExited(MouseEvent me)
                    {
                        PopUp.setVisible(false);
                    }
            });
            x+=164;
            MainP.add(stoly[i]);
        }
        x=320;
        JLabel talieText = new JLabel();
        Labels.setLabel(talieText, 400, 520, 400, 30, "TALIE do wyboru:", font1);
        talieText.setOpaque(false);
        MainP.add(talieText);
        for(int i=0;i<talie_nazwy.length; i++)
        {
            talie[i] = new JButton();
            talie[i].setText(talie_nazwy[i]);
            talie[i].setName(""+(i+1));
            talie[i].setBounds(x, 550, 160, 30);
            talie[i].addMouseListener(new MouseAdapter(){
                    public void mouseClicked(MouseEvent me)
                    {
                        JButton button = (JButton) me.getComponent();
                        talia=Integer.parseInt(button.getName());
                    }
                    public void mouseEntered(MouseEvent me)
                    {
                        PopUp.removeAll();
                        JButton button = (JButton) me.getComponent();
                        int talia_nr=Integer.parseInt(button.getName());
                        PopUpImage.setIcon(new ImageIcon(getClass().getResource(("/taliav3/img/Talie/T0"+(talia_nr)+"/D3.png"))));
                        JLabel PopUpImage2 = new JLabel();
                        ImageIcon icon = new ImageIcon(getClass().getResource(("/taliav3/img/Talie/T0"+(talia_nr)+"/CQ.png")));
                        PopUpImage2.setIcon(icon);
                        JLabel PopUpImage3 = new JLabel();
                        icon = new ImageIcon(getClass().getResource(("/taliav3/img/Talie/T0"+(talia_nr)+"/joker.png")));
                        PopUpImage3.setIcon(icon);
                        PopUp.add(PopUpImage);
                        PopUp.add(PopUpImage2);
                        PopUp.add(PopUpImage3);
                        PopUp.show(MainW, 1000, 700-6-icon.getIconHeight()*3);
                    }
                    public void mouseExited(MouseEvent me)
                    {
                        PopUp.setVisible(false);
                    }
            });
            x+=164;
            MainP.add(talie[i]);
        }
        MainP.add(exit);
        MainP.add(minimize);
        MainP.add(titlebar);
        MainP.add(BG);
        MainP.repaint();
        for(int i=0; i<MainP.getComponentCount(); i++)
        {
            MainP.getComponent(i).addKeyListener(new KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                switch(evt.getKeyCode())
                {
                    case KeyEvent.VK_F1:
                        restoreMenu();
                        break;
                    case KeyEvent.VK_ESCAPE:
                        restoreMenu();
                        break;
                }
            }});
        }
        MainW.addKeyListener(new KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                switch(evt.getKeyCode())
                {
                    case KeyEvent.VK_F1:
                        restoreMenu();
                        break;
                    case KeyEvent.VK_ESCAPE:
                        restoreMenu();
                        break;
                }
            }});
    }
    protected void help(int Stage)
    {
        MainP.removeAll();
        JLabel img = new JLabel();
        JLabel next = new JLabel();
        JLabel previous = new JLabel();

        previous.setBounds(786,578,44,45);
        if(Stage>1 && Stage<5)
            previous.addMouseListener(MouseAdapters.helpNavigatePrevious(Stage, this));
        next.setBounds(831,547,80,77);
        next.addMouseListener(MouseAdapters.helpNavigateNext(Stage, this));

        Labels.setLabel(img, 0, 0, 1142,700, ("/taliav3/img/Help/help"+Stage+".jpg"));
        exit.addMouseListener(MouseAdapters.closeFrame());
        MainP.add(exit);
        MainP.add(minimize);
        MainP.add(titlebar);
        MainP.add(next);
        MainP.add(previous);
        MainP.add(img);
        MainP.repaint();
        MainW.addKeyListener(new KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                switch(evt.getKeyCode())
                {
                    case KeyEvent.VK_F1:
                        restoreMenu();
                        break;
                    case KeyEvent.VK_ESCAPE:
                        restoreMenu();
                        break;
                }
            }
        });
    }
    protected void restoreMenu()
    {
        MainP.removeAll();
        for(int i=0; i<6; i++)
            MainP.add(menu[i]);
        MainP.add(exit);
        MainP.add(minimize);
        MainP.add(titlebar);
        MainP.add(BG);
        MainP.repaint();
        try
        {
           MainW.removeKeyListener(MainW.getKeyListeners()[0]);
        }
        catch(Exception e){};
        MainW.addKeyListener(new KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                switch(evt.getKeyCode())
                {
                    case KeyEvent.VK_F1:
                        help(1);
                        break;
                    case KeyEvent.VK_ESCAPE:
                        restoreMenu();
                        break;
                }
            }
        });
	}
    protected void ServOrClient()
    {
        MainP.removeAll();
        server = new JLabel();
        klient = new JLabel();
        ok = new JLabel();
        SerwerPathArea = new JTextField(serw);
        SerwerPathArea.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(checkIP(SerwerPathArea.getText()))
                {
                    boolean black=false;
                    if(path.equals("/taliav3/img/Stoly/stol4.jpg"))
                        black=true;
                    MainW.dispose();
                    DrawTable table = new DrawTable(2,path,black,talia,rewers,jokers,false,serwerPath,port);                    
                }
            }
        });
        SerwerPathArea.addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyReleased(KeyEvent e) {
                JTextField backup = (JTextField) e.getComponent();
                if(checkIP(backup.getText()))
                    ok.setIcon(new ImageIcon(getClass().getResource("/taliav3/img/ok.png")));
                else
                    ok.setIcon(new ImageIcon(getClass().getResource("/taliav3/img/fail.png")));                            
            }
        });        
        Labels.setLabel(server, 340, 240, 128, 192, "/taliav3/img/serwer.png");
        Labels.setLabel(klient, 608, 240, 128, 192, "/taliav3/img/klient.png");

        SerwerPathArea.setBounds(530, 320, 300, 40);
        Labels.setLabel(ok, 860, 310, 73, 73, "/taliav3/img/ok.png");
        Font font1 = new Font("Century Gothic", Font.PLAIN, 30);
        SerwerPathArea.setFont(font1);
        MainP.add(exit);
        MainP.add(minimize);
        MainP.add(titlebar);
        MainP.add(server);
        MainP.add(klient);
        MainP.add(BG);
        server.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                serwer=true;
                boolean black=false;
                if(path.equals("/taliav3/img/Stoly/stol4.jpg"))
                    black=true;
                MainW.dispose();
                System.out.println("talia= "+talia);
                System.out.println("path= "+path);
                System.out.println("rewers= "+rewers);
                DrawTable table = new DrawTable(2,path,black,talia,rewers,jokers,serwer,"",port);
            }
        });
        
        klient.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                serwer=false;
                MainP.remove(BG);
                MainP.remove(klient);
                klient.removeMouseListener(klient.getMouseListeners()[0]);
                server.removeMouseListener(server.getMouseListeners()[0]);
                server.setIcon(new ImageIcon(getClass().getResource("/taliav3/img/klient.png")));
                MainP.add(SerwerPathArea);
                MainP.add(ok);
                ok.addMouseListener(new MouseAdapter() 
                {
                    public void mouseClicked(MouseEvent me)
                    {
                        if(checkIP(SerwerPathArea.getText()))
                        {
                            boolean black=false;
                            if(path.equals("/taliav3/img/Stoly/stol4.jpg"))
                                black=true;
                            MainW.dispose();
                            DrawTable table = new DrawTable(2,path,black,talia,rewers,jokers,false,serwerPath,port);
                        }
                    }
                });
                MainP.add(BG);
                MainP.repaint();
            }
        });
        MainP.repaint();
    }
    private boolean checkIP(String ipAddress)
    {
        if(ipAddress.equals("localhost"))
            return true;
	String[] parts = ipAddress.split( "\\." );    
	if ( parts.length != 4 )    
            return false;
        for ( String s : parts )
	{   
            try
            {
                int i = Integer.parseInt( s );        
                if ( (i < 0) || (i > 255) )                   
                    return false;
            }
            catch(NumberFormatException ex)
            {
                return false;
            }            
	}    
	return true;
    }
}
